/**
 * 
 */
package info.niwota.home.fm;

import instant.openvfs.home.Debug;
import x.java.io.File;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public class ApplicationShellActivity extends ShellActivity {
	
	private static final String TAG = "ApplicationShellActivity";
	private File file;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		init(savedInstanceState);
	}

	private void init(Bundle savedInstanceState) {
		setTheme(android.R.style.Theme_NoDisplay);
		//
		setResult(RESULT_CANCELED); // default
		//
		Uri data = getIntent().getData();
		try {
			String[] pa = Utils.parse(data);
			if (Debug.DEBUG) {
				Log.d(TAG, this + " data: " + data + " command: " + pa[0]
						+ " path: " + pa[1]);
			}
			//
			String cmd = pa[0];
			file = new File(pa[1]);
			if (perform(cmd, file)) {
				finish();
			}
		} catch (ActivityNotFoundException e) {
			Toast.makeText(this, "Cannot handle " + data, Toast.LENGTH_SHORT);
			finish();
		} catch (Exception e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
			finish();
		}
	}
	
	protected void doFinish() {
		finish();
	}
	
	@Override
	protected void refresh(File file) {
		if (Debug.DEBUG) {
			Log.d(TAG, this + " refresh: " + file);
		}
		Uri uri = Uri.parse("content://v:" + file.getPath());
		Intent intent = new Intent();
		intent.setData(uri);
		boolean norefresh = (getCallingPackage() != null);
		if (norefresh) {
			setResult(RESULT_OK, intent);
			return;
		}
	}

	@Override
	protected File getSelectedFile() {
		return file;
	}

	@Override
	protected File getContextFile(int pos) {
		return null;
	}
}
